#include "lovelight.h"

static int g_delay=0;
static int g_skip_frame=0;
static int frames;	




    
void playasc(void)
{
    /*the bin file is ll.bin*/
    
    char *str;
    int total_frames;
    int i;
    int time;
	int desireFrame;
	
    double fps;
    char fpstext[50];
    
    int skipped=0;
    COORD pos={0,0};
    HANDLE stdo=GetStdHandle(STD_OUTPUT_HANDLE);
    
    total_frames=uncompress(&str);

      
    playbgm();
      
    frames=0;
    
    while(frames<=total_frames){
        
        /*-----------------------------*/
        time=getMusicPosition();

        fps=(double)frames/(double)time*1000.0;
        sprintf(fpstext,"Bad Apple fps=%.0f",fps);
        SetConsoleTitle(fpstext);
        
		desireFrame=(time-T_OFFSET)/DURATION*FRAMES;
		
		if(frames>desireFrame){
			g_delay=1;
			g_skip_frame=0;

		}else{
			g_delay=0;

			g_skip_frame=1;
		}
		
		
		/*--------------------------------*/
        
        /*frame control here*/
        
        if(!g_skip_frame){
            
            /*system("cls");*/
            SetConsoleCursorPosition(stdo, pos);
            printf("%s",str+frames*25*80);
             
        } else{
            ++skipped;
        }
               
       
        while(g_delay){
			        /*-----------------------------*/
            time=getMusicPosition();
    
    
    		desireFrame=(time-T_OFFSET)/DURATION*FRAMES;
    		
    		if(frames>desireFrame){
    			g_delay=1;
    			g_skip_frame=0;
    
    		}else{
    			g_delay=0;
    
    			g_skip_frame=1;
    		}
		
		/*--------------------------------*/
		}
        
        ++frames;
        
    }
    
    free(str);
    
    printf("skipped frames=%d\n",skipped);
}        
        
